package com.sansi.bleFloodlight.activity;

import android.Manifest;

import android.content.DialogInterface;

import android.os.Bundle;
import android.os.Handler;

import com.my.httpapi.api.annotation.Layout;
import com.my.httpapi.api.baseView.BaseActivity;
import com.sansi.bleFloodlight.R;

import com.sansi.bleFloodlight.utils.CommentUtils;
import com.sansi.bleFloodlight.utils.RouterPath;
import com.sansi.bleFloodlight.view.CustomDialog;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


@Layout(R.layout.layout_activity_splash)
public class SplashActivity extends BaseActivity {

    @Override
    protected void initView() {
        //进行相关权限的申请
        getPermissions();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
    }

    @Override
    protected void destory() {

    }

    @Override
    protected String showTitle() {
        return null;
    }

    @Override
    protected boolean statusBarIsBlack() {
        return false;
    }

    @Override
    protected boolean showLeftArrow() {
        return false;
    }

    private void getPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION)
                    .subscribe(new Observer<Boolean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Boolean aBoolean) {
                            //被拒绝重新申请直到同意
                            if (!aBoolean) {
                                showDialog();
                            } else {
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        RouterPath.gotoTab();
                                        finish();
                                      //  ceshi();

                                    }
                                }, 1000);
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    private void showDialog() {
        new CustomDialog.Builder(this).setTitle("Tips")
                .setMessage("Permission has been denied, please set permission")
                .setPositiveButton("Exit app", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                }).setNegativeButton("Set permissions", new DialogInterface.OnClickListener() {


            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                CommentUtils.openPermission(SplashActivity.this);
                finish();
            }
        }).create().show();
    }
}
